<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 工序分类数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\ReviewTaskService;
use common\service\StepCategoryService;
use common\service\StepService;
use think\Exception;
use think\Request;
use think\Response;
use Throwable;

class StepCategoryController extends VerifyController
{
    /**
     * @var StepService
     */
    protected $stepService;
    private $reviewTaskService;
    private $stepCategoryService;

    public function __construct()
    {
        parent::__construct();
        $this->stepService = StepService::getInstance();
        $this->reviewTaskService = ReviewTaskService::getInstance();
        $this->stepCategoryService = StepCategoryService::getInstance();
    }

    /**
     * 获取工序分类从属字典数据列表
     * @param Request $request
     * @return Response
     */
    public function getStepCategoryList(Request $request)
    {
        $resData = $this->stepService->getStepCategoryList();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 绑定实体模块
     * @param Request $request
     * @return Response|void
     * @throws \Exception
     */
    public function bindEntityModule(Request $request)
    {
        try {
            $resData = $this->stepService->bindEntityModule($request->param());
            return json($resData);
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询排期计划
     * @param Request $request
     * @return Response
     */
    public function getStepCategoryPlan(Request $request)
    {
        $resData = $this->stepService->getStepCategoryPlan($request->param());
        return json($resData);
    }

    /**
     * 所有工序数据树
     * @param Request $request
     * @return Response
     */
    public function getStepCategoryListWithStep(Request $request)
    {
        $resData = $this->stepService->getAllStepTree($request->param());
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 更新工序分类排序索引
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function setStepCategoryIndex(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->stepService->setStepCategoryIndex($param['data']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 播放列表分类数据
     * @param Request $request
     * @return Response
     */
    public function getPlayListStepCategoryData(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $dateSource = $request->param()['param']['data_source'] ?? '';
        $episodeId = $filter['episode_id'] ?? 0;
        $isNormalCategory = $filter['is_normal_category'] ?? 'no';
        try {
            $resData = $this->reviewTaskService->getPlayListStepCategoryData($filter['module_code'], $filter['project_id'], $episodeId, $dateSource, $isNormalCategory, $request->getUserId());
            return json(success_response('', $resData));
        } catch (Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 前置配置工序列表
     * @param Request $request
     * @return Response
     */
    public function getProjectTemplateStepCategoryList(Request $request)
    {
        $resData = $this->stepService->getProjectTemplateStepCategoryList();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 合并模块 所包含的二级分类查询
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getShowStepCategory(Request $request)
    {
        $resData = $this->stepService->getShowStepCategory(false);
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 查询二级分类列表
     * @param Request $request
     * @return Response
     */
    public function secondLevelList(Request $request)
    {
        $resData = $this->stepCategoryService->secondLevelList();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 查询展示的分类列表
     * @param Request $request
     * @return Response
     */
    public function showSecondLevelList(Request $request)
    {
        $resData = $this->stepCategoryService->showSecondLevelList();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 查询项目模板展示的分类列表
     * @param Request $request
     * @return Response
     */
    public function showProjectSecondLevelList(Request $request)
    {
        $resData = $this->stepCategoryService->ShowProjectSecondLevelList();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 上游环节关系
     * @param Request $request
     * @return Response
     */
    public function upstreamRelation(Request $request)
    {
        $filter = $request->param()['param']['filter'] ?? [];
        $resData = $this->stepCategoryService->upstreamRelation($filter);
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 根据实体分类工序分类列表ids
     * @param Request $request
     * @return Response
     */
    public function getStepCategoryListByEntity(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->stepCategoryService->getStepCategoryListByEntity($param['entity_module_code']);
        return json(success_response('Query was successful.', $resData));
    }
}
